package com.gavin.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBUtil
{
	private Connection conn = null;

	public Connection getConnection()
	{
		try
		{
			// System.out.println("创建数据库连接！当前为："+ connNumber);

			Class.forName(ConfigLoaderUtil.CLASS_NAME);
			String url = ConfigLoaderUtil.DATABASE_URL + "//"
					+ ConfigLoaderUtil.SERVER_IP + ":"
					+ ConfigLoaderUtil.SERVER_PORT + "/"
					+ ConfigLoaderUtil.DATABASE_SID;
			try
			{
				// 生成链接
				conn = DriverManager.getConnection(url,
						ConfigLoaderUtil.USERNAME, ConfigLoaderUtil.PASSWORD);
			} catch (SQLException e)
			{
				e.printStackTrace();
			}
		} catch (ClassNotFoundException e)
		{
			e.printStackTrace();
		}
		return conn;
	}

	public void close()
	{
		if (conn != null)
		{
			try
			{
				conn.close();
				conn = null;
			} catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
	}

	public static void close(Connection conn)
	{
		if (conn != null)
		{
			try
			{
				conn.close();
				conn = null;
			} catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
	}

	public static Connection closeConn(Connection conn)
	{
		if (conn != null)
		{
			try
			{
				conn.close();
				conn = null;
			} catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
		return conn;
	}

	public static void close(PreparedStatement pstmt)
	{
		try
		{
			if (pstmt != null)
			{
				pstmt.close();
				pstmt = null;
			}
		} catch (SQLException e)
		{
			e.printStackTrace();
		}
	}

	public static void close(ResultSet rs)
	{
		try
		{
			if (rs != null)
			{
				rs.close();
				rs = null;
			}
		} catch (SQLException e)
		{
			e.printStackTrace();
		}
	}
}
